# @Time:   2021/2/25 17:26
# @Author: Zong Wei

from time import sleep

from faker import Faker
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.wait import WebDriverWait

from ui_automation_testing.test.pageObject.loginPage import LoginPage
from ui_automation_testing.utils.logger import Logger


class BizPartnerControlPage(LoginPage):
    logger = Logger(__name__).get_log()
    menu = (By.XPATH, "//ul[@class='site-menu']//span[.='业务通用']")
    sub_menu = (By.XPATH, "//ul[@class='site-menu']//span[.='业务伙伴管控']")
    search_button = (By.ID, "lr-search")
    add_button = (By.ID, "lr-add")
    edit_button = (By.ID, "lr-edit")
    del_button = (By.ID, "lr-delete")
    save_button = (By.ID, "lr-save")
    clear_button = (By.ID, "lr-clear")
    add_conf_button = (By.ID, "btnadd")
    edit_target = (By.ID, "datagrid-row-r2-2-0")
    edit_conf_button = (By.XPATH, "//span[.='批量更改配置管控']")

    partner_name = (By.ID, "PartnerNameCN")
    role = (By.ID, "Role")
    role_value = (By.XPATH, "//div[@id='Role-option'] //li[2]")
    status = (By.ID, "ControlState")
    status_value = (By.XPATH, "//div[@id='ControlState-option'] //li[2]")
    msg = (By.ID, "ControlMsg")

    confirm_button = (By.XPATH, "//a[.='确认']")
    first_row = (By.XPATH, "//span[.='母公司']//following::tr[1]")

    def __init__(self):
        self.driver = LoginPage().login()
        self.wait = WebDriverWait(self.driver, 10)

    def biz_partner_ctrl_home(self):
        self.logger.info("登录系统成功")
        self.wait.until(ec.element_to_be_clickable(self.menu)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.sub_menu)).click()
        sleep(1)
        return self.driver

    def search_partner(self):
        self.driver.switch_to.frame(1)
        self.wait.until(ec.presence_of_element_located(self.partner_name)).send_keys('上海静初化工物流有限公司')
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.search_button)).click()
        sleep(2)
        self.wait.until(ec.element_to_be_clickable(self.clear_button)).click()
        self.logger.info("查询完成")

    def add_edit_config(self):
        self.search_partner()
        self.wait.until(ec.element_to_be_clickable(self.first_row)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.add_conf_button)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.edit_target)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.edit_conf_button)).click()
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame(2)
        self.wait.until(ec.element_to_be_clickable(self.role)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.role_value)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.status)).click()
        sleep(1)
        self.wait.until(ec.element_to_be_clickable(self.status_value)).click()
        sleep(1)
        self.wait.until(ec.presence_of_element_located(self.msg)).send_keys(Faker().address())
        sleep(1)
        self.driver.switch_to.default_content()
        self.wait.until(ec.element_to_be_clickable(self.confirm_button)).click()
        self.logger.info("更改配置管控完成")


if __name__ == '__main__':
    pg = BizPartnerControlPage()
    pg.biz_partner_ctrl_home()
    pg.add_edit_config()

